/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeans;

import entity.Rss;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import sessionBeans.RssFacadeLocal;

/**
 *
 * @author Minh Luc
 */
@ManagedBean
@SessionScoped
public class RssManagement {

    @EJB
    private RssFacadeLocal rssFacade;
    private Rss rss = new Rss();
    private Rss selectRss;
    private List<Rss> list;
    private List<Rss> filteredList;

    public void delete() {
        FacesMessage msg = null;
        try {
            rssFacade.remove(selectRss);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Delete Rss", "Delete Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage() + " Delete unSuccessful");
        }
        list = getRssList();
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void create() {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        try {
            rss.setUsers(System.user);
            rssFacade.create(rss);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Create Rss", "Create Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage() + " Duplicate Rss");
        }
        rss = new Rss();
        list = getRssList();
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("create", true);
    }

    public void edit() {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        try {
            rssFacade.edit(selectRss);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Edit Rss", "Edit Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage());
        }
        list = getRssList();
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("edit", true);
    }

    public Rss getRss() {
        return rss;
    }

    public void setRss(Rss rss) {
        this.rss = rss;
    }

    public Rss getSelectRss() {
        return selectRss;
    }

    public void setSelectRss(Rss selectRss) {
        this.selectRss = selectRss;
    }

    public List<Rss> getList() {
        list = getRssList();
        return list;
    }

    public void setList(List<Rss> list) {
        this.list = list;
    }

    public List<Rss> getFilteredList() {
        return filteredList;
    }

    public void setFilteredList(List<Rss> filteredList) {
        this.filteredList = filteredList;
    }

    private List<Rss> getRssList() {
        if (System.user != null) {
            return rssFacade.findByUserId(System.user.getUserId());
        }
        return null;
    }

    //Validate Rss
    private boolean validate(Rss rss) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        if (rss.getRssDesciption() == null || rss.getRssDesciption().length() == 0) { //BankName
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "RssTitle is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (rss.getRssDesciption().length() < 2 || rss.getRssDesciption().length() > 30) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "RssTile is from 2 - 30 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(rss.getRssDesciption())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "RssTile is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (rss.getRssLink() == null || rss.getRssLink().length() == 0) { //BankName
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "RssLink is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (rss.getRssLink().length() < 2 || rss.getRssLink().length() > 300) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "RssLink is from 2 - 300 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isRssLink(rss.getRssLink())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Format RssLink is http://xxx.xxx.xx");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        return true;
    }
}
